package com.ipan.weixin.msg.send;

import java.util.ArrayList;
import java.util.List;

import com.ipan.weixin.msg.general.XmlMsg;

/**
 * 发送图文消息
 * 
 * @author iPan
 * @version 2013-12-25
 */
public class NewsSendMsg extends AbstractSendJsonMsg {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 5112848118398313368L;
	
	private News news;
	
	public NewsSendMsg(String touser) {
		super(touser, XmlMsg.TYPE_NEWS);
	}

	public NewsSendMsg() {
		super(XmlMsg.TYPE_NEWS);
	}

	public News getNews() {
		return news;
	}
	
	public void setNews(News news) {
		this.news = news;
	}

	public void addArticle(String title, String description, String url, String picurl) {
		checkNews();
		news.addArticle(title, description, url, picurl);
	}
	
	public void addArticle(Article article) {
		checkNews();
		news.addArticle(article);
	}
	
	private synchronized void checkNews() {
		if (news == null) {
			news = new News();
		}
	}

	class News {
		
		private List<Article> articles;

		public List<Article> getArticles() {
			return articles;
		}

		public void setArticles(List<Article> articles) {
			this.articles = articles;
		}
		
		public void addArticle(Article article) {
			check();
			articles.add(article);
		}
		
		public void addArticle(String title, String description, String url, String picurl) {
			check();
			articles.add(new Article(title, description, url, picurl));
		}
		
		private synchronized void check() {
			if (articles == null) {
				articles = new ArrayList<Article>();
			}
		}
	}
	
	class Article {
		private String title;
		private String description;
		private String url;
		private String picurl;
		
		public Article() {}
		
		public Article(String title, String description, String url, String picurl) {
			this.title = title;
			this.description = description;
			this.url = url;
			this.picurl = picurl;
		}
		public String getTitle() {
			return title;
		}
		public void setTitle(String title) {
			this.title = title;
		}
		public String getDescription() {
			return description;
		}
		public void setDescription(String description) {
			this.description = description;
		}
		public String getUrl() {
			return url;
		}
		public void setUrl(String url) {
			this.url = url;
		}
		public String getPicurl() {
			return picurl;
		}
		public void setPicurl(String picurl) {
			this.picurl = picurl;
		}
		
	}

//	public static void main(String[] args) {
//		NewsServiceMsg msg = new NewsServiceMsg("OPENID");
//		msg.addArticle("Happy Day", "Is Really A Happy Day", "URL", "PIC_URL");
//		msg.addArticle("Happy Day", "Is Really A Happy Day", "URL", "PIC_URL");
//		System.out.println(msg);
//	}

}

/*
{
"touser":"OPENID",
"msgtype":"news",
"news":{
    "articles": [
     {
         "title":"Happy Day",
         "description":"Is Really A Happy Day",
         "url":"URL",
         "picurl":"PIC_URL"
     },
     {
         "title":"Happy Day",
         "description":"Is Really A Happy Day",
         "url":"URL",
         "picurl":"PIC_URL"
     }
     ]
}
}
*/